<template>
  <div class="app-container">

    <el-row style="margin-bottom: 10px;">
      <el-button @click="submit" class="pan-btn success-btn" icon="el-icon-s-promotion" style="float: right;" type="success"
        size="mini">
        {{ isUpdate ? '重新发送' : isReregist ? ('发送至' + fromUserName) : '发送' }}
      </el-button>

      <el-tooltip effect="dark" :content="isFullLast ? '不填充' : '自动填充'" placement="top">
        <el-switch :disabled="fullLastDisabled" style="float: right;
          margin-right: 40px;
          margin-top: 8px;"
          v-model="isFullLast" @change="changeFullLast" active-color="#7cbd36" inactive-color="#ff4949" inactive-text="上一单">
        </el-switch>
      </el-tooltip>
    </el-row>

    <el-row :gutter="12">
      <!-- 基本信息头 -->
      <el-col :span="12">
        <el-card shadow="hover" class="card2">
          <h4><b>基本信息头</b></h4>
          <el-form label-width="120px">
            <el-form-item label="物料名称:">
              <el-form v-if="product != ''" :inline="true">
                <el-form-item v-if="showUpdateProductName">
                  <el-tooltip content="点击改为'(通用)'" placement="top">
                    <el-button @click="updateProductName" size="mini">{{product.productName}}</el-button>
                  </el-tooltip>
                </el-form-item>
                <el-form-item v-else>
                  {{product.productName}}
                </el-form-item>
                <el-form-item label="GMP编码:">
                  {{product.productGmp}}
                </el-form-item>
                <el-form-item label="货品规格:">
                  <el-input id="productSpec" v-model="product.productSpec" placeholder="货品规格" size="small" />
                </el-form-item>
                <br />
                <el-form-item label="贮存条件:">
                 <el-select v-model="checkList.storageConditions" size="small">
                   <el-option v-for="storage in storageConditions" :key="storage.id" :label="storage.val" :value="storage.val" />
                  </el-select>
                </el-form-item>
                <br />
                <el-form-item>
                  <el-button :disabled="isReregist || isUpdate" type="info" class="pan-btn info-btn" @click="showProductSelect"
                    size="mini">
                    重新选择
                  </el-button>
                </el-form-item>
              </el-form>
              <el-button v-if="product == ''" type="info" class="pan-btn info-btn" @click="showProductSelect" size="mini">
                请选择
              </el-button>
            </el-form-item>

            <el-form-item label="原厂批号:">
              <el-input id="originalBatch" v-model="yfbList.originalBatch" placeholder="原厂批号" size="small" />
            </el-form-item>

            <el-form-item label="物料编码:">
              <el-input id="batchNum" v-model="checkList.batchNum" placeholder="物料编码" size="small"  @blur="setBatchNum">
              </el-input>
            </el-form-item>

            <el-form-item label="存货编码:">
              <el-input v-model="yfbList.stockNum" placeholder="存货编码" size="small">
              </el-input>
            </el-form-item>

<!--            <el-form-item label="包装规格:">
              <el-autocomplete id="productPackSpec" v-model="checkList.productPackSpec" @change="setProductPackSpec"
                :fetch-suggestions="queryProductPackSpec" placeholder="包装规格" size="small" style="width: 100%;" />
            </el-form-item> -->

            <el-form-item label="包装规格:" prop="productPackSpec">
            	<div v-if="productPackSpecList.length == 0" style="color: #F56C6C;">
            		<el-input size="small" v-model="checkList.productPackSpec" placeholder="当前货品在货品管理中未添加包装规格,请手动输入">
            		</el-input>
            	</div>
            	<el-select v-else id="productPackSpec2" filterable allow-create v-model="checkList.productPackSpec"
            		placeholder="请选择" size="small">
            		<el-option v-for="item in productPackSpecList" :key="item.productPackSpecId"
            			:label="item.productPackSpec" :value="item.productPackSpec"  allow-create filterable/>
            	</el-select>
            </el-form-item>

            <el-form-item label="接收数量:">
              <el-input id="pleaseCheckQuantity" v-model="yfbList.pleaseCheckQuantity" @change="setpleaseCheckQuantity" placeholder="接收数量(数值)" size="small">
               <el-select id="grossUnitId" @change="setGrossUnit" value-key="unitId" v-model="dataForm.grossUnit" slot="append" placeholder="单位" style="width: 100px;">
                 <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>

            <el-form-item label="请验数量:">
              <el-input id="grossQuantity" v-model="yfbList.grossQuantity" @change="setGrossQuantity" placeholder="请验数量(数值)" size="small">
                <el-select id="grossUnitId" @change="setGrossUnit" value-key="unitId" v-model="dataForm.grossUnit" slot="append" placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>

            <el-form-item>
              <span slot="label">接收件数:</span>
              <el-input id="pleaseCheckPiece" v-model="yfbList.pleaseCheckPiece" @change="setPleaseCheckPiece" placeholder="件数(数值)" size="small">
                <el-select id="grossUnitId" v-model="yfbList.pleaseUnitName" value-key="unitId" slot="append" placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in jsUnitList" :key="item.unitId" :label="item.unitName" :value="item.unitName" />
                </el-select>
              </el-input>
            </el-form-item>



            <el-form-item>
              <span slot="label">请验件数:</span>
              <el-input id="grossPiece" v-model="yfbList.grossPiece" @change="setGrossPiece" placeholder="请验件数(数值)" size="small">
                <el-select id="grossUnitId" v-model="yfbList.pleaseUnitName"  value-key="unitId" slot="append" placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in jsUnitList" :key="item.unitId" :label="item.unitName" :value="item.unitName" />
                </el-select>
              </el-input>
            </el-form-item>
            <!--

            <el-form-item label="接收数量:">
              <el-input id="grossQuantity" v-model="yfbList.grossQuantity" @change="setGrossQuantity" placeholder="接收数量(数值)"
                size="small">
                <el-select id="grossUnitId" @change="setGrossUnit" value-key="unitId" v-model="dataForm.grossUnit" slot="append"
                  placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>

            <el-form-item label="请验数量:">
              <el-input id="pleaseCheckQuantity" v-model="yfbList.pleaseCheckQuantity" @change="setpleaseCheckQuantity"
                placeholder="请验数量(数值)" size="small">
                <el-select id="grossUnitId" @change="setGrossUnit" value-key="unitId" v-model="dataForm.grossUnit" slot="append"
                  placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>

            <el-form-item>
              <span slot="label">接收件数:</span>
              <el-input id="grossPiece" v-model="yfbList.grossPiece" @change="setGrossPiece" placeholder="件数(数值)" size="small">
                <template slot="append">
                  <div style="width: 59px;">件</div>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <span slot="label">请验件数:</span>
              <el-input id="pleaseCheckPiece" v-model="yfbList.pleaseCheckPiece" @change="setPleaseCheckPiece"
                placeholder="件数(数值)" size="small">
                <template slot="append">
                  <div style="width: 59px;">件</div>
                </template>
              </el-input>
            </el-form-item> -->

            <el-form-item label="包装容器:">
              <el-radio-group v-model="dataForm.packContainerId" @change="setPackContainer" size="small">
                <el-radio-button v-for="item in packContainerList" :key="item.packContainerId" :label="item.packContainerId">{{item.packContainerName}}</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="请验目的:">
              <el-radio-group v-model="dataForm.checkPurposeId" @change="setCheckPurpose" size="small">
                <el-radio-button v-for="item in checkPurposeList" :key="item.checkPurposeId" :label="item.checkPurposeId">{{item.checkPurposeName}}</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="上市状态:">
              <el-radio-group v-model="checkList.listedState" size="small">
                <el-radio-button :key='1' :label='1'>已上市</el-radio-button>
                <el-radio-button :key='0' :label='0'>未上市</el-radio-button>
              </el-radio-group>
              <div class="right" style="float: right;margin-right: 70%;">
                  <el-tooltip class="item" effect="dark" content="上市状态将影响到放行单的版本" placement="right-start">
                    <i class="el-icon-warning-outline">信息</i>
                  </el-tooltip>
                  </div>
            </el-form-item>

            <el-form-item label="直接放行物料:" v-show="checkList.listedState==1">
              <el-radio-group v-model="checkList.fastPass" size="small">
                <el-radio-button :key='0' :label='0'>否</el-radio-button>
                <el-radio-button :key='1' :label='1'>是</el-radio-button>
              </el-radio-group>
<!--              <div class="right" style="float: right;margin-right: 70%;">
                  <el-tooltip class="item" effect="dark" content="上市状态将影响到放行单的版本" placement="right-start">
                    <i class="el-icon-warning-outline">信息</i>
                  </el-tooltip>
                  </div> -->
            </el-form-item>


          </el-form>
        </el-card>
      </el-col>
      <!-- 基本信息尾 -->
      <el-col :span="12">


        <el-card shadow="hover" class="card2">
          <h4><b>供应商</b></h4>
          <el-form label-width="140px">
            <el-form-item>
              <span slot="label">供&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;应&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;商:</span>
              <el-select id="supplierId" v-model="dataForm.supplier" value-key="supplierId" @change="setSupplier"
                filterable remote reserve-keyword :remote-method="searchSupplier" placeholder="关键词搜索" :loading="supplierLoading"
                style="width: 100%;" size="small">
                <el-option v-for="item in supplierList" :key="item.supplierId" :label="item.supplierName" :value="item">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <span slot="label">供 应 商 物 料 名称:</span>
              <el-input id="originalProductName" v-model="yfbList.originalProductName" placeholder="供应商物料名称" size="small" />
            </el-form-item>
						
			<el-form-item>
			  <span slot="label">供 应 商 物 料 规格:</span>
			  <el-input id="originalProductSpec" v-model="yfbList.originalProductSpec" placeholder="供应商物料规格" size="small" />
			</el-form-item>

            <el-form-item>
              <span slot="label">经 批 准 的 供 应商:</span>
              <el-radio-group v-model="yfbList.isSupplierAuthorized" size="small">
                <el-radio-button :label="0">是</el-radio-button>
                <el-radio-button :label="1">否</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="批准文号/登记号:">
              <el-input id="approvalNum" v-model="yfbList.approvalNum" placeholder="批准文号/登记号" size="small" />
            </el-form-item>

            <el-form-item label="随货文件:">
              <span slot="label">随&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;货&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;文&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;件:</span>
              <el-checkbox-group v-model="followFileIds" @change="yfbList.followFileIds = followFileIds.join(',')" size="small">
                <el-checkbox-button v-for="item in followFileList" :label="item.followFileId" :key="item.followFileId">{{item.followFileName}}</el-checkbox-button>
              </el-checkbox-group>
            </el-form-item>

          </el-form>
        </el-card>
        <br />
        <el-card shadow="hover" class="card2">
          <h4><b>生产企业</b></h4>
          <el-form label-width="120px">
            <el-form-item>
              <span slot="label">生&nbsp;&nbsp;产&nbsp;&nbsp;企&nbsp;&nbsp;&nbsp;业:</span>
              <el-select id="manufacturerId" v-model="dataForm.manufacturer" value-key="manufacturerId" @change="setManufacturer"
                filterable remote reserve-keyword :remote-method="searchManufacturer" placeholder="关键词搜索" :loading="manufacturerLoading"
                style="width: 100%;" size="small">
                <el-option v-for="item in manufacturerList" :key="item.manufacturerId" :label="item.manufacturerName"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <span slot="label">生&nbsp;&nbsp;产&nbsp;&nbsp;日&nbsp;&nbsp;&nbsp;期:</span>
              <el-date-picker v-if="productionDateUnit == 0" @change="notifyProDate" id="productionDate" v-model="yfbList.productionDate"
                align="right" type="date" placeholder="精确到日" :picker-options="pickerOptions" style="width: 93%;" format="yyyyMMdd"
                value-format="yyyy-MM-dd HH:mm:ss" size="small">
              </el-date-picker>
              <el-date-picker v-if="productionDateUnit == 1" @change="notifyProDate" id="productionDate" v-model="yfbList.productionDate"
                align="right" type="month" placeholder="精确到月" :picker-options="pickerOptions" style="width: 93%;"
                format="yyyyMM" value-format="yyyy-MM-dd HH:mm:ss" size="small">
              </el-date-picker>
              <el-tooltip :content="productionDateUnitName" placement="top">
                <el-switch v-model="productionDateUnitName" active-color="#13ce66" inactive-color="#ff4949"
                  @click.native="changeProductionDateUnit()" active-value="日" inactive-value="月" size="mini">
                </el-switch>
              </el-tooltip>
            </el-form-item>

            <el-form-item>
              <el-select slot="label" v-model="select" @change="changeSelect" size="small">
                <el-option label="有效期" value="validityDate">有效期</el-option>
                <el-option label="复验期" value="reinspectDate">复验期</el-option>
              </el-select>
              <el-date-picker @change="notifyValiDate" id="selectDate" v-model="selectDate" :type="selectDateUnit == 0 ? 'date' : 'month'"
                :placeholder="selectDateUnit == 0 ? '精确到日' : '精确到月'" :picker-options="{
                  disabledDate(time) {
                    return time.getTime() < Date.now()
                  }
                }"
                style="width: 93%;" :format="selectDateUnit == 0 ? 'yyyyMMdd' : 'yyyyMM'" value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
              <el-tooltip :content="selectDateUnitName" placement="top">
                <el-switch v-model="selectDateUnitName" active-color="#13ce66" inactive-color="#ff4949" @click.native="changeSelectDateUnit()"
                  active-value="日" inactive-value="月" size="mini">
                </el-switch>
              </el-tooltip>
            </el-form-item>

            <el-form-item label="包装异常请注明,必要时附照片">
              <span slot="label">包装异常请注明,必要时附照片</span>
              <el-input type="textarea" v-model="yfbList.packExceptionInfo">
              </el-input>
            </el-form-item>
          </el-form>
        </el-card>

      </el-col>
    </el-row>

    <el-row :gutter="12" style="margin-top: 20px;">
      <el-col :span="12">
        <el-card shadow="hover" class="card2">
          <h4><b>基本信息尾</b></h4>
          <el-form label-width="110px">
            <el-form-item v-if="product != ''">
              <span slot="label">请&nbsp;&nbsp;检&nbsp;&nbsp;项&nbsp;&nbsp;&nbsp;目:</span>
              <div v-if="allItems.length == 0" style="color: #F56C6C;">请添加此货品的检验标准</div>
              <div v-else>
                <el-radio-group v-model="checkList.isAllTest" @change="changeItems" size="small">
                  <el-radio-button :label="0">全检</el-radio-button>
                  <el-radio-button :label="1">其他</el-radio-button>
                </el-radio-group>
                <el-button style="margin-left: 20px;" type="info" class="pan-btn info-btn" @click="showStandardDrawer"
                  size="mini">
                  查看项目
                </el-button>
              </div>
            </el-form-item>

            <el-form-item>
              <span slot="label">初&nbsp;&nbsp;验&nbsp;&nbsp;结&nbsp;&nbsp;&nbsp;论:</span>
              <el-radio-group v-model="yfbList.firstVerdict" size="small">
                <el-radio-button :label="0">符合规定</el-radio-button>
                <el-radio-button :label="1">不符合规定</el-radio-button>
                <el-radio-button :label="2">其他</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item v-if="yfbList.firstVerdict == 2">
              <span slot="label">符&nbsp;&nbsp;合&nbsp;&nbsp;数&nbsp;&nbsp;&nbsp;量:</span>
              <el-input id="productConformQuantity" v-model="yfbList.productConformQuantity" @change="setProductConformQuantity"
                placeholder="符合规定数量(数值)" size="small">
                <template slot="append">{{ yfbList.grossUnitName }}</template>
              </el-input>
              <el-input id="productNotConformQuantity" v-model="yfbList.productNotConformQuantity" @change="setProductNotConformQuantity"
                placeholder="不符合规定数量(数值)" size="small">
                <template slot="append">{{ yfbList.grossUnitName }}</template>
              </el-input>
            </el-form-item>

            <el-form-item label="物料进厂日期:">
              <el-date-picker id="comeDate" v-model="yfbList.comeDate" align="right" type="date" placeholder="选择日期"
                :picker-options="pickerOptions" style="width: 100%;" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd HH:mm:ss"
                size="small">
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <span slot="label">物&nbsp;料&nbsp;接&nbsp;收人:</span>
              <el-select id="comeUserId" v-model="dataForm.comeUser" value-key="userId" @change="setComeUser"
                filterable placeholder="物料接收人" style="width: 100%;" size="small">
                <el-option v-for="item in userList" :key="item.userId" :label="item.name + '(' + item.username + ')'"
                  :value="item">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <span slot="label">请&nbsp;&nbsp;验&nbsp;&nbsp;部&nbsp;&nbsp;&nbsp;门:</span>
              {{checkList.registCheckDepartmentName}}
            </el-form-item>

            <el-form-item>
              <span slot="label">请&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;验&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;人:</span>
              {{checkList.registCheckUserName}}
            </el-form-item>

            <el-form-item>
              <span slot="label">请&nbsp;&nbsp;验&nbsp;&nbsp;日&nbsp;&nbsp;&nbsp;期:</span>
              <el-date-picker id="registCheckDate" v-model="checkList.registCheckDate" align="right" type="date"
                placeholder="选择日期" :picker-options="pickerOptions" style="width: 100%;" format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd HH:mm:ss" size="small">
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <span slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</span>
              <el-input v-model="checkList.remark" placeholder="备注" size="small" />
            </el-form-item>

          </el-form>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover" class="card2">
          <h4><b>相关信息</b></h4>
          <el-form label-width="240px">
            <el-form-item label="送货单和采购订单数量:">
              <span slot="label">送&nbsp;&nbsp;货&nbsp;&nbsp;单&nbsp;&nbsp;和&nbsp;&nbsp;采&nbsp;&nbsp;购&nbsp;&nbsp;订&nbsp;&nbsp;单&nbsp;&nbsp;数
                量:</span>
              <el-input id="deliveryAndBuyQuantity" v-model="yfbList.deliveryAndBuyQuantity" @change="setDeliveryAndBuyQuantity"
                placeholder="送货单和采购订单数量(数值)" size="small">
                <el-select id="deliveryAndBuyUnitId" @change="setDeliveryAndBuyUnit" value-key="unitId" v-model="dataForm.deliveryAndBuyUnit"
                  slot="append" placeholder="单位" style="width: 100px;">
                  <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item" />
                </el-select>
              </el-input>
            </el-form-item>

            <el-form-item label="接收数量与送货单和采购订单相符:">
              <el-radio-group v-model="yfbList.isGetQuantityEqDeliveryAndBuy" size="small">
                <el-radio-button :label="0">是</el-radio-button>
                <el-radio-button :label="1">否</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="每批物料均有合格检验报告书:">
              <span slot="label">每 批 物 料 均 有 合 格检验报告书:</span>
              <el-radio-group v-model="yfbList.haveReport" size="small">
                <el-radio-button :label="0">是</el-radio-button>
                <el-radio-button :label="1">否</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="是否需要冷冻冷藏:">
              <span slot="label">是否需要 冷 冻 冷 藏:</span>
              <el-radio-group v-model="yfbList.needRefrigerate" size="small">
                <el-radio-button :label="0">是</el-radio-button>
                <el-radio-button :label="1">否</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="是否符合冷冻冷藏要求:">
              <span slot="label">是否符合 冷 冻 冷 藏 要 求:</span>
              <el-radio-group v-model="yfbList.isConformFreeze" size="small">
                <el-radio-button :label="0">是</el-radio-button>
                <el-radio-button :label="1">否</el-radio-button>
                <el-radio-button :label="2">不涉及</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="外包装清洁无破损,标识清晰:">
              <span slot="label">外包装清洁无破损,标识清晰:</span>
              <el-radio-group v-model="yfbList.isIntact" size="small">
                <el-radio-button :label="0">是</el-radio-button>
                <el-radio-button :label="1">否</el-radio-button>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="实物批号与报告书批号一致:">
              <span slot="label">实物批号与报告书批号一致:</span>
              <el-radio-group v-model="yfbList.isIdentical" size="small">
                <el-radio-button :label="0">是</el-radio-button>
                <el-radio-button :label="1">否</el-radio-button>
              </el-radio-group>
            </el-form-item>

          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="12" style="margin-top: 20px;">
      <!-- 相关信息 -->
      <el-col :span="12">

      </el-col>

      <el-col :span="12">
      </el-col>
    </el-row>

    <product-select @getProduct="getProduct" @showProductSelect="showProductSelect" :childDataForm="childDataForm" v-if="productSelectVisible" />

    <standard-drawer ref="standard" :childDataForm="childDataForm" @close="closeStandardDrawer" @getStandard="getStandard"
      v-if="standardDrawerVisible" />

  </div>
</template>

<script>
  import {
    mapGetters
  } from 'vuex'
  import {
    CODE_OK
  } from '@/common/js/config'
  import {
    isBlank,
    parseTime,
    parseTimeForCN
  } from '@/common/js/utils'
  import {
    isNumber
  } from '@/common/js/validate'
  import ProductSelect from '@/views/modules/base/product-select'
  import StandardDrawer from '@/views/modules/base/standard-drawer'
  import {
    getUnitList,
    getPackContainerList,
    getCheckPurposeList,
    getFollowFileList,
    getUserList,
    getPackStatusList,
    getPackExceptionList,
    registCheck,
    getRegistCheckInfo,
    getLastCheckForRegist,
    getProductPackSpecByProductId,
    checkBatchNum
  } from '@/api/check'
  import {
    getSupplierList
  } from "@/api/supplier"
  import {
    getManufacturerList
  } from "@/api/manufacturer"
  import {
    getStandardByProductId
  } from '@/api/standard'
  import {
    getStorageConditionsByProductId,
    getProductPackSpecsByProductId,
    getProductItemMaps,
    updateProductName
  } from '@/api/product'
  export default {
    name: 'registcheck304',
    components: {
      ProductSelect,
      StandardDrawer
    },
    data() {
      return {
        storageConditions:[],
        productPackSpecList:[],
        printCheckVersion: '04',
        fullLastDisabled: true,
        isFullLast: false,
        //被退回标志
        isReregist: false,
        //发起后未取样修改标志
        isUpdate: false,
        fromUserName: null,
        fromStatus: null,
        checkReturnId: null,
        unitList: [],
        jsUnitList:[
			{unitId:'0',unitName:'件'},
			{unitId:'1',unitName:'桶'},
			{unitId:'2',unitName:'箱'},
			{unitId:'3',unitName:'托'},
			{unitId:'4',unitName:'袋'}
        ],
        packContainerList: [],
        checkPurposeList: [],
        followFileList: [],
        userList: [],
        supplierLoading: false,
        supplierList: [],
        manufacturerLoading: false,
        manufacturerList: [],
        packStatusList: [],
        packExceptionList: [],
        standard: '',
        allItems: [],
        items: [],
        followFileIds: [],
        packStatusIds: [],
        packExceptionIds: [],
        productSelectVisible: false,
        standardDrawerVisible: false,
        checkList: {
          checkTypeId: 3,
          checkTypeName: '包材',
          registCheckDepartmentId: '',
          registCheckDepartmentName: '',
          registCheckUserId: '',
          registCheckUserName: '',
          isTest: 0,
          isAllTest: 1,
          status: 1,
          productId: '',
          productName: '',
          productGmp: '',
          productSpec: '',
          productPackSpec: '',
          registCheckDate: '',
          listedState:1,
          fastPass:0,
		  batchNum:""
        },
        yfbList: {
          pleaseUnitName:'件',
          followFileIds: '',
          packStatusIds: '',
          packExceptionIds: '',
          validityDate: '',
          reinspectDate: '',
          comeUserId: '',
          comeUserName: ''
        },
        product: '',
        dataForm: {
          grossUnit: '',
          deliveryAndBuyUnit: '',
          packContainerId: '',
          checkPurposeId: '',
          comeUser: '',
          supplier: '',
          manufacturer: '',
          runningTimeArr: [new Date(), new Date()],
          productGmpPrefix: '',
          batchNumSuffix: ''
        },
        select: 'validityDate',
        selectDateUnit: 1,
        selectDateUnitName: '月',
        productionDateUnit: 0,
        productionDateUnitName: '日',
        selectDate: '',
        childDataForm: '',
        productPackSpecs: [],
        queryedProductPackSpecs: false
      }
    },
    created() {
      if (!isBlank(this.$route.query.checkId)) {
        this.isReregist = true
        this.isUpdate = this.$route.query.checkReturnId == 0
      }
      this.init()
      this.getYfbInfo()
    },
    computed: {
      ...mapGetters([
        'userInfo'
      ]),
      showUpdateProductName() {
        if (this.product == null || this.product == '') {
          return false
        }
        return this.product.productName.indexOf('说明书') > -1 && this.product.productName.indexOf('通用') == -1
      }
    },
    methods: {
      initStorageConditions(productId){
        let params = {
          productId:productId
        }
        getStorageConditionsByProductId(params).then(res=>{
          if (res.code === CODE_OK) {
          	this.storageConditions = res.storageConditions
          	if (this.storageConditions.length == 0) {
          		this.$message.info("此货品未在货品管理中添加贮存条件")
          	}
          } else {
          	this.$message.error(res.msg)
          }
        });
      },
      nbAppend(){
        let subGmp = this.product.productGmp;
        if(subGmp!=undefined){
          if(subGmp.substring(0,2) == "NB"){
            return "托"
          }else{
            return "件"
          }
        }
      },
      notifyProDate() {
        if (!isBlank(this.yfbList.productionDate)) {
          let date = parseTimeForCN(this.yfbList.productionDate, this.productionDateUnit)
          this.$confirm('生产日期为 <' + date + '> ?', '提示', {
            confirmButtonText: '是的',
            cancelButtonText: '重新输入',
            type: 'warning'
          }).then(() => {
            let diff = this.moment(this.selectDate).diff(new Date(), 'days')
            if (diff <= 0) {
              this.$message.error('所选日期不能小于当月')
              this.selectDate = null
            } else if (diff < 365) {
              this.$confirm('所选日期确认在一年之内 ?', '提示', {
                confirmButtonText: '是的',
                cancelButtonText: '错了',
                type: 'warning'
              }).then(() => {}).catch(() => {
                this.selectDate = null
              })
            }
          }).catch(() => {
            this.yfbList.productionDate = null
          })
        }
      },
      notifyValiDate() {
        if (!isBlank(this.selectDate)) {
          let date = parseTimeForCN(this.selectDate, this.selectDateUnit)
          this.$confirm('有效期为 <' + date + '> ?', '提示', {
            confirmButtonText: '是的',
            cancelButtonText: '重新输入',
            type: 'warning'
          }).then(() => {
            let diff = this.moment(this.selectDate).diff(new Date(), 'days')
            if (diff <= 0) {
              this.$message.error('所选日期不能小于当月')
              this.selectDate = null
            } else if (diff < 365) {
              this.$confirm('所选日期确认在一年之内 ?', '提示', {
                confirmButtonText: '是的',
                cancelButtonText: '错了',
                type: 'warning'
              }).then(() => {}).catch(() => {
                this.selectDate = null
              })
            }
          }).catch(() => {
            this.selectDate = null
          })
        }
      },
      init() {
        if (this.isReregist) {
          const params = {
            checkId: this.$route.query.checkId,
            content: 'yfb'
          }
          getRegistCheckInfo(params).then(res => {
            if (res.code === CODE_OK) {
              this.fillData(res.info)
              this.setReregistInfo()
            } else {
              this.$message({
                message: res.msg,
                type: 'error',
                duration: 1000,
                onClose: () => {
                  window.history.go(-1)
                }
              })
            }
          })
        } else {
          this.setRegistInfo()
        }
      },
      setRegistInfo() {
        this.dataForm.comeUser = {
          userId: this.userInfo.userId,
          name: this.userInfo.name
        }
        this.setComeUser()
        this.checkList.registCheckDepartmentId = this.userInfo.departmentId
        this.checkList.registCheckDepartmentName = this.userInfo.departmentName
        this.checkList.registCheckUserId = this.userInfo.userId
        this.checkList.registCheckUserName = this.userInfo.name
      },
      setpleaseCheckQuantity() {
        if (!isNumber(this.yfbList.pleaseCheckQuantity)) {
          this.$message.error('请输入数值类型')
          this.yfbList.pleaseCheckQuantity = null
        }else if(!isNumber(this.yfbList.deliveryAndBuyQuantity)){
          this.$set(this.yfbList,'deliveryAndBuyQuantity',JSON.parse(JSON.stringify(this.yfbList.pleaseCheckQuantity)))
        }
      },
      fillData(info) {
        let checkList = info.checkList
        this.initStorageConditions(checkList.productId)
        let yfbList = info.yfbList
        this.checkList = JSON.parse(JSON.stringify(checkList))
        this.checkList.fastPass = 0
        this.yfbList = JSON.parse(JSON.stringify(yfbList))
        let val = isBlank(yfbList.validityDate) ? 'reinspectDate' : 'validityDate'
        this.selectDate = this.yfbList[val]
        this.select = val
        this.dataForm.grossUnit = {
          unitId: yfbList.grossUnitId,
          unitName: yfbList.grossUnitName
        }
        this.dataForm.packContainerId = yfbList.packContainerId
        this.dataForm.checkPurposeId = yfbList.checkPurposeId
        this.dataForm.comeUser = {
          userId: yfbList.comeUserId,
          name: yfbList.comeUserName
        }
        this.dataForm.supplier = {
          supplierId: yfbList.supplierId,
          supplierName: yfbList.supplierName
        }
        this.dataForm.manufacturer = {
          manufacturerId: yfbList.manufacturerId,
          manufacturerName: yfbList.manufacturerName
        }
        this.searchSupplier(this.dataForm.supplier.supplierName)
        this.searchManufacturer(this.dataForm.manufacturer.manufacturerName)
        this.followFileIds = yfbList.followFileIds.split(",").map(value => {
          return parseInt(value)
        })
        this.packStatusIds = yfbList.packStatusIds.split(",").map(value => {
          return parseInt(value)
        })
        if (!isBlank(yfbList.packExceptionIds)) {
          this.packExceptionIds = yfbList.packExceptionIds.split(",").map(value => {
            return parseInt(value)
          })
        }
        this.dataForm.deliveryAndBuyUnit = {
          unitId: this.yfbList.deliveryAndBuyUnitId,
          unitName: this.yfbList.deliveryAndBuyUnitName
        }
        if (!isBlank(this.yfbList.runningTimeStart) && !isBlank(this.yfbList.runningTimeEnd)) {
          this.dataForm.runningTimeArr = [this.yfbList.runningTimeStart, this.yfbList.runningTimeEnd]
        }
        // 如果是修改或者重新发起，没有必要再获取货品和标准
        if (this.isReregist) {
          this.getProduct(info.product, 'noGetStandard')
          this.standard = info.baseStandard
          this.allItems = this.standard.items
          this.items = info.items
          this.checkList.isAllTest = checkList.isAllTest
        } else {
          // 如果是获取上一单，那么需要获取最新标准，和上一单标准比较
          this.checkList.isAllTest = null
          this.items = []
          if (this.isFullLast && this.standard.insideInspectionStandardId != info.standard.insideInspectionStandardId) {
            if (checkList.isAllTest == 0) {
              this.checkList.isAllTest = 0
              this.items = JSON.parse(JSON.stringify(this.allItems))
            } else {
              this.$message.error("标准已升级，请自己选择检验项目")
            }
          } else {
            if (checkList.isAllTest == 0) {
              this.checkList.isAllTest = 0
              this.items = JSON.parse(JSON.stringify(this.allItems))
            } else {
              // 按名字选择
              for (let i = 0, j = info.items.length; i < j; i++) {
                let it = this.allItems.find(v => v.inspectionItemName == info.items[i].inspectionItemName)
                if (isBlank(it)) {
                  this.$message.error("项目未找到，请自行选择")
                  this.checkList.isAllTest = null
                  this.items = []
                  return
                } else {
                  this.items.push(JSON.parse(JSON.stringify(it)))
                }
              }
              this.checkList.isAllTest = 1
            }
          }
        }
        if (!isBlank(checkList.productSpec)) {
          this.product.productSpec = checkList.productSpec
        }
        this.setBatchNum()
      },
      setReregistInfo() {
        if (!this.isUpdate) {
          this.fromUserName = this.$route.query.fromUserName
          this.fromStatus = this.$route.query.fromStatus
          this.checkReturnId = this.$route.query.checkReturnId
        }
      },
      getYfbInfo() {
        getUnitList().then(res => {
          if (res.code === CODE_OK) {
            this.unitList = res.list
          } else {
            this.$message.error(res.msg)
          }
        })
        const params = {
          checkTypeId: 1,
          printVersion: '04'
        }
        getPackContainerList(params).then(res => {
          if (res.code === CODE_OK) {
            this.packContainerList = res.list
          } else {
            this.$message.error(res.msg)
          }
        })
        getCheckPurposeList(params).then(res => {
          if (res.code === CODE_OK) {
            this.checkPurposeList = res.list
          } else {
            this.$message.error(res.msg)
          }
        })
        getFollowFileList(params).then(res => {
          if (res.code === CODE_OK) {
            this.followFileList = res.list
          } else {
            this.$message.error(res.msg)
          }
        })
        getUserList({
          departmentId: this.userInfo.departmentId
        }).then(res => {
          if (res.code === CODE_OK) {
            this.userList = res.list
          } else {
            this.$message.error(res.msg)
          }
        })
        getPackStatusList(params).then(res => {
          if (res.code === CODE_OK) {
            this.packStatusList = res.list
          } else {
            this.$message.error(res.msg)
          }
        })
        getPackExceptionList(params).then(res => {
          if (res.code === CODE_OK) {
            this.packExceptionList = res.list
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      submit() {
        let val = this.select
        this.yfbList[val] = this.selectDate
        this.yfbList.selectDateUnit = this.selectDateUnit
        this.yfbList.productionDateUnit = this.productionDateUnit
        this.checkList.isMerge = 1
        this.checkList.printCheckVersion = this.printCheckVersion
        if (this.formVerify()) {
          const data = {
            content: 'yfb',
            fromStatus: this.fromStatus,
            checkReturnId: this.checkReturnId,
            checkList: this.checkList,
            yfbList: this.yfbList,
            product: this.product,
            standard: this.standard,
            items: this.items
          }
          registCheck(data).then(res => {
            if (res.code === CODE_OK) {
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000,
                onClose: () => {
                  window.history.go(-1)
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      },
      changeFullLast() {
        if (this.isFullLast) {
          const params = {
            content: 'yfb',
            productId: this.product.productId
          }
          getLastCheckForRegist(params).then(res => {
            if (res.code === CODE_OK) {
              this.fillData(res.info)
              this.setRegistInfo()
              this.searchSupplier(this.dataForm.supplier.supplierName)
              this.searchManufacturer(this.dataForm.manufacturer.manufacturerName)
            } else {
              this.$message.error(res.msg)
              this.isFullLast = false
            }
          })
        } else {
          //初始化

        }
      },
      changeSelect() {
        this.yfbList.validityDate = null
        this.yfbList.reinspectDate = null
      },
      formVerify() {
        if (isBlank(this.checkList.storageConditions)) {
          this.$message.error('请选择货品贮存条件')
          return false
        }
        if (isBlank(this.product)) {
          this.$message.error('请选择货品')
          return false
        }
        if (isBlank(this.yfbList.originalBatch)) {
          this.$message.error('请输入原厂批号')
          $("#originalBatch").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#originalBatch").removeAttr('style')
        if (isBlank(this.checkList.batchNum)) {
          this.$message.error('请输入物料编码')
          $("#batchNum").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#batchNum").removeAttr('style')
        if (isBlank(this.checkList.productPackSpec)) {
          this.$message.error('请输入包装规格')
          $("#productPackSpec").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#productPackSpec").removeAttr('style')
        if (isBlank(this.yfbList.grossQuantity)) {
          this.$message.error('请输入请验数量(数值)')
          $("#grossQuantity").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#grossQuantity").removeAttr('style')
        if (isBlank(this.yfbList.grossUnitId)) {
          this.$message.error('请选择请验数量(单位)')
          $("#grossUnitId").css({
            "border-color": "#D08585"
          })
          return false
        }

        $("#grossUnitId").removeAttr('style')
        if (isBlank(this.yfbList.pleaseCheckQuantity)) {
          this.$message.error('请输入接收数量(数值)')
          $("#pleaseCheckQuantity").css({
            "border-color": "#D08585"
          })
          return false
        }

        $("#pleaseCheckQuantity").removeAttr('style')
        if (isBlank(this.yfbList.grossUnitId)) {
          this.$message.error('请选择接收数量(单位)')
          $("#grossUnitIds").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#grossUnitIds").removeAttr('style')
        if (isBlank(this.yfbList.grossPiece)) {
          this.$message.error('请输入请验件数')
          $("#grossPiece").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#grossPiece").removeAttr('style')
        if (isBlank(this.yfbList.pleaseCheckPiece)) {
          this.$message.error('请输入件数')
          $("#pleaseCheckPiece").css({
            "border-color": "#D08585"
          })
          return false
        }
        // $("#productPackSpec").removeAttr('style')
        // if (isBlank(this.yfbList.grossQuantity)) {
        //   this.$message.error('请输入接收数量(数值)')
        //   $("#grossQuantity").css({
        //     "border-color": "#D08585"
        //   })
        //   return false
        // }
        // $("#grossQuantity").removeAttr('style')
        // if (isBlank(this.yfbList.grossUnitId)) {
        //   this.$message.error('请选择接收数量(单位)')
        //   $("#grossUnitId").css({
        //     "border-color": "#D08585"
        //   })
        //   return false
        // }

        // $("#grossUnitId").removeAttr('style')
        // if (isBlank(this.yfbList.pleaseCheckQuantity)) {
        //   this.$message.error('请输入请验数量(数值)')
        //   $("#pleaseCheckQuantity").css({
        //     "border-color": "#D08585"
        //   })
        //   return false
        // }

        // $("#pleaseCheckQuantity").removeAttr('style')
        // if (isBlank(this.yfbList.grossUnitId)) {
        //   this.$message.error('请选择接收数量(单位)')
        //   $("#grossUnitIds").css({
        //     "border-color": "#D08585"
        //   })
        //   return false
        // }
        // $("#grossUnitIds").removeAttr('style')
        // if (isBlank(this.yfbList.grossPiece)) {
        //   this.$message.error('请输入件数')
        //   $("#grossPiece").css({
        //     "border-color": "#D08585"
        //   })
        //   return false
        // }
        // $("#grossPiece").removeAttr('style')
        // if (isBlank(this.yfbList.pleaseCheckPiece)) {
        //   this.$message.error('请输入请验件数')
        //   $("#pleaseCheckPiece").css({
        //     "border-color": "#D08585"
        //   })
        //   return false
        // }
        $("#pleaseCheckPiece").removeAttr('style')
        if (isBlank(this.yfbList.packContainerId)) {
          this.$message.error('请选择包装容器')
          return false
        }
        if (isBlank(this.yfbList.checkPurposeId)) {
          this.$message.error('请选择请检目的')
          return false
        }
        if (isBlank(this.checkList.listedState)) {
          this.$message.error('请选择上市状态')
          return false
        }
        if (this.items.length == 0) {
          this.$message.error('请选择请检项目')
          return false
        }
        if (typeof this.yfbList.firstVerdict != 'number') {
          this.$message.error('请选择初检结论')
          return false
        } else if (this.yfbList.firstVerdict == 2) {
          if (!isNumber(this.yfbList.productConformQuantity)) {
            this.$message.error('请输入符合规定数量')
            $("#productConformQuantity").css({
              "border-color": "#D08585"
            })
            return false
          }
          if (!isNumber(this.yfbList.productNotConformQuantity)) {
            this.$message.error('请输入不符合规定数量')
            $("#productNotConformQuantity").css({
              "border-color": "#D08585"
            })
            return false
          }
        }
        $("#productConformQuantity").removeAttr('style')
        $("#productNotConformQuantity").removeAttr('style')
        if (isBlank(this.yfbList.comeDate)) {
          this.$message.error('请选择物料进厂日期')
          $("#comeDate").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#comeDate").removeAttr('style')
        if (isBlank(this.yfbList.comeUserId)) {
          this.$message.error('请选择物料接收人')
          $("#comeUserId").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#comeUserId").removeAttr('style')
        if (isBlank(this.checkList.registCheckDepartmentId) || isBlank(this.checkList.registCheckUserId)) {
          this.$message.error('登录过期,请刷新页面')
          return false
        }
        if (isBlank(this.checkList.registCheckDate)) {
          this.$message.error('请选择收料日期')
          $("#registCheckDate").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#registCheckDate").removeAttr('style')
        if (isBlank(this.yfbList.supplierId)) {
          this.$message.error('请选择供应商')
          $("#supplierId").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#supplierId").removeAttr('style')
        if (isBlank(this.yfbList.originalProductName)) {
          this.$message.error('请输入供应商物料名称')
          $("#originalProductName").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#originalProductName").removeAttr('style')
        if (typeof this.yfbList.isSupplierAuthorized != 'number') {
          this.$message.error('请选择供应商是否经批准')
          $("#isSupplierAuthorized").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#isSupplierAuthorized").removeAttr('style')
        if (isBlank(this.yfbList.approvalNum)) {
          this.$message.error('请输入批准文号/登记号')
          $("#approvalNum").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#approvalNum").removeAttr('style')
        if (isBlank(this.yfbList.followFileIds)) {
          this.$message.error('请选择随货文件')
          return false
        }
        if (isBlank(this.yfbList.manufacturerId)) {
          this.$message.error('请选择生产企业')
          $("#manufacturerId").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#manufacturerId").removeAttr('style')
        if (isBlank(this.yfbList.deliveryAndBuyQuantity)) {
          this.$message.error('请输入送货单和采购订单数量')
          $("#deliveryAndBuyQuantity").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#deliveryAndBuyQuantity").removeAttr('style')
        if (!isNumber(this.yfbList.deliveryAndBuyUnitId)) {
          this.$message.error('请选择送货单和采购订单数量的单位')
          $("#deliveryAndBuyUnitId").css({
            "border-color": "#D08585"
          })
          return false
        }
        $("#deliveryAndBuyUnitId").removeAttr('style')
        if (typeof this.yfbList.isGetQuantityEqDeliveryAndBuy != 'number') {
          this.$message.error('请选择接收数量是否与送货单和采购订单相符')
          return false
        }
        if (typeof this.yfbList.haveReport != 'number') {
          this.$message.error('请选择每批物料是否均有合格检验报告书')
          return false
        }
        if (typeof this.yfbList.isConformFreeze != 'number') {
          this.$message.error('请选择物料是否符合冷冻冷藏要求')
          return false
        }
        if (typeof this.yfbList.needRefrigerate != 'number') {
          this.$message.error('请选择物料是否需要冷冻冷藏')
          return false
        }
        if (typeof this.yfbList.isIntact != 'number') {
          this.$message.error('请选择物料外包装清洁无破损,标识清晰')
          return false
        }
        if (typeof this.yfbList.isIdentical != 'number') {
          this.$message.error('请选择物料实物批号与报告书批号一致')
          return false
        }
        return true
      },
      changeItems() {
        if (this.checkList.isAllTest == 0) {
          this.items = JSON.parse(JSON.stringify(this.allItems))
        } else {
          this.items = []
          this.showStandardDrawer()
        }
      },
      showProductSelect() {
        this.childDataForm = {
          checkTypeId: this.checkList.checkTypeId
        }
        this.productSelectVisible = !this.productSelectVisible
      },
      showStandardDrawer() {
        this.childDataForm = {
          product: this.product,
          standard: this.standard,
          allItems: this.allItems,
          checkedItems: this.items
        }
        this.standardDrawerVisible = !this.standardDrawerVisible
      },
      closeStandardDrawer() {
        this.childDataForm = ''
        this.standardDrawerVisible = !this.standardDrawerVisible
      },
      getStandard(standardObj) {
        this.items = standardObj.checkedItems
        this.checkList.isAllTest = standardObj.isAllTest
      },
      getProduct(product, opt) {
        this.product = product
        if (this.showUpdateProductName) {
          this.$message({
            message: '货品名称改为通用？试着点击一下货品名称吧！',
            type: 'warning',
            duration: 5000
          })
        }
        if (isBlank(this.yfbList.originalProductName)) {
          this.$set(this.yfbList, 'originalProductName', product.productName)
        }
        // 设置 productGmpPrefix
        if (product.haveSupplierNumber == 0) {
          this.dataForm.productGmpPrefix = product.productGmp.substring(0, this.product.productGmp.length - 1)
        } else if (product.haveVersionNumber == 0) {
          this.dataForm.productGmpPrefix = product.productGmp.substring(0, this.product.productGmp.length - 2)
        } else {
          this.dataForm.productGmpPrefix = product.productGmp + '-'
        }
        if (this.isReregist || this.isUpdate) {
          //this.dataForm.batchNumSuffix = this.checkList.batchNumPrefix.replace(this.dataForm.productGmpPrefix, '')
          this.dataForm.batchNumSuffix = this.checkList.batchNumPrefix
        } else {
          this.checkList.batchNum = this.checkList.batchNumPrefix = null
          this.dataForm.batchNumSuffix = null
        }
        if (!isBlank(this.checkList.batchNumPrefix)) {
          //this.dataForm.batchNumSuffix = this.checkList.batchNumPrefix.replace(this.dataForm.productGmpPrefix, '')
          this.dataForm.batchNumSuffix = this.checkList.batchNumPrefix
        }
        if (opt != 'noGetStandard') {
          const params = {
            productId: product.productId
          }
          this.initStandard()
          getStandardByProductId(params).then(res => {
            if (res.code === CODE_OK) {
              this.allItems = res.standard.items
              if (this.allItems.length == 0) return
              if (!this.isReregist) {
                this.standard = {
                  insideInspectionStandardId: res.standard.insideInspectionStandardId,
                  insideInspectionStandardNum: res.standard.insideInspectionStandardNum,
                  nationInspectionStandardNum: res.standard.nationInspectionStandardNum,
                  accordingToInside: res.standard.accordingToInside,
                  accordingToNation: res.standard.accordingToNation
                }
                this.fullLastDisabled = this.isUpdate ? true : false
              }
              params.standardId = res.standard.insideInspectionStandardId
              getProductItemMaps(params).then(res => {
                if (res.code === CODE_OK) {
                  for (let bpim of res.list) {
                    for (let i = 0, j = this.allItems.length; i < j; i++) {
                      if (this.allItems[i].inspectionItemId == bpim.inspectionItemId) {
                        this.allItems[i].insideInspectionItemValue = bpim.insideInspectionItemValue
                        this.allItems[i].nationInspectionItemValue = bpim.nationInspectionItemValue
                        break
                      }
                    }
                  }
                } else {
                  this.$message.error(res.msg)
                }
              })
              this.initStorageConditions(product.productId)
              getProductPackSpecsByProductId(params).then(res => {
              	if (res.code === CODE_OK) {
              		this.productPackSpecList = res.productPackSpecs
              		if (this.productPackSpecList.length == 0) {
              			this.$message.info("此货品未在货品管理中添加包装规格，请在请验时手动输入。")
              		}
              	} else {
              		this.$message.error(res.msg)
              	}
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      },
      updateProductName() {
        let productName = this.product.productName + '(通用)'
        const data = {
          productId: this.product.productId,
          productName: productName
        }
        updateProductName(data).then(res => {
          if (res.code === CODE_OK) {
            this.$message.success('修改成功')
            this.product.productName = productName
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      initStandard() {
        this.standard = ''
        this.allItems = []
        this.items = []
        this.checkList.isAllTest = ''
      },
      queryProductPackSpec(queryString, cb) {
        if (!this.queryedProductPackSpecs && this.product != '') {
          const params = {
            productId: this.product.productId
          }
          getProductPackSpecByProductId(params).then(res => {
            if (res.code === CODE_OK) {
              if (res.list.length != 0) {
                for (let i = 0, j = res.list.length; i < j; i++) {
                  this.productPackSpecs.push({
                    value: res.list[i]
                  })
                }
              }
              this.queryedProductPackSpecs = true
            } else {
              this.$message.error(res.msg)
            }
          })
        } else if (this.productPackSpecs.length != 0) {
          this.productPackSpecs = this.productPackSpecs.filter(ps => {
            return ps.value.indexOf(queryString) > -1
          })
        }
        cb(this.productPackSpecs)
      },
      //供应商生产企业筛选
      searchSupplier(query) {
        this.supplierLoading = true
        this.supplierList = []
        if (!isBlank(query)) {
          const params = {
            page: 1,
            limit: 10,
            supplierName: query
          }
          getSupplierList(params).then(res => {
            if (res.code === CODE_OK) {
              this.supplierList = res.page.list
            } else {
              this.$message.error(res.msg)
            }
            this.supplierLoading = false
          })
        }
      },
      searchManufacturer(query) {
        this.manufacturerLoading = true
        this.manufacturerList = []
        if (!isBlank(query)) {
          const params = {
            page: 1,
            limit: 10,
            manufacturerName: query
          }
          getManufacturerList(params).then(res => {
            if (res.code === CODE_OK) {
              this.manufacturerList = res.page.list
            } else {
              this.$message.error(res.msg)
            }
            this.manufacturerLoading = false
          })
        }
      },
      //根据object赋值id和name
      setIsConformFreeze() {
        this.$set(this.yfbList, 'haveRecordTAndH', JSON.parse(JSON.stringify(this.yfbList.isConformFreeze)))
        this.$set(this.yfbList, 'isPlaceOk', JSON.parse(JSON.stringify(this.yfbList.isConformFreeze)))
      },
      setRunningTime() {
        if (this.dataForm.runningTimeArr.length > 0) {
          this.yfbList.runningTimeStart = this.dataForm.runningTimeArr[0]
          this.yfbList.runningTimeEnd = this.dataForm.runningTimeArr[1]
        } else {
          this.yfbList.runningTimeStart = null
          this.yfbList.runningTimeEnd = null
        }
      },
      changeProductionDateUnit() {
        this.productionDateUnit = this.productionDateUnitName == '月' ? 1 : 0
      },
      changeSelectDateUnit() {
        this.selectDateUnit = this.selectDateUnitName == '月' ? 1 : 0
      },
      setBatchNum() {
				const params = {
					checkTypeId: this.checkList.checkTypeId,
					batchNum: this.checkList.batchNum,
				}
				checkBatchNum(params).then(res => {
					if (res.code === CODE_OK) {
						if (res.reCount > 0) {
              this.$alert('已存在【'+res.reCount+'】条相同物料编码的请验单信息,请确认是否重复发起.', '提示', {
                confirmButtonText: '确定'
              });
						}
					}else{
						this.$message.error('检查物料编码错误，请联系管理员')
					}
				})
      },
      setProductPackSpec() {
        this.checkList.productPackSpec = this.checkList.productPackSpec.replace(/KG/g, 'kg')
      },
      setGrossQuantity() {
        if (!isNumber(this.yfbList.grossQuantity)) {
          this.$message.error('请输入数值类型')
          this.yfbList.grossQuantity = null
        } else if (!isNumber(this.yfbList.deliveryAndBuyQuantity)) {
          this.$set(this.yfbList, 'deliveryAndBuyQuantity', JSON.parse(JSON.stringify(this.yfbList.grossQuantity)))
        }
      },
      setDeliveryAndBuyQuantity() {
        if (!isNumber(this.yfbList.deliveryAndBuyQuantity)) {
          this.$message.error('请输入数值类型')
          this.yfbList.deliveryAndBuyQuantity = null
        }
      },
      setProductConformQuantity() {
        if (!isNumber(this.yfbList.productConformQuantity)) {
          this.$message.error('请输入数值类型')
          this.yfbList.productConformQuantity = null
        }
      },
      setProductNotConformQuantity() {
        if (!isNumber(this.yfbList.productNotConformQuantity)) {
          this.$message.error('请输入数值类型')
          this.yfbList.productNotConformQuantity = null
        }
      },
      setGrossUnit() {
        this.yfbList.grossUnitId = this.dataForm.grossUnit.unitId
        this.yfbList.grossUnitName = this.dataForm.grossUnit.unitName
        let u = {
          unitId: this.yfbList.grossUnitId,
          unitName: this.yfbList.grossUnitName
        }
        this.setDeliveryAndBuyUnit()
      },
      setDeliveryAndBuyUnit() {
        this.yfbList.deliveryAndBuyUnitId = this.dataForm.deliveryAndBuyUnit.unitId
        this.yfbList.deliveryAndBuyUnitName = this.dataForm.deliveryAndBuyUnit.unitName
      },
      setGrossPiece() {
        if (!isNumber(this.yfbList.grossPiece)) {
          this.$message.error('请输入数值类型')
          this.yfbList.grossPiece = null
        }
      },
      setPleaseCheckPiece() {
        if (!isNumber(this.yfbList.pleaseCheckPiece)) {
          this.$message.error('请输入数值类型')
          this.yfbList.pleaseCheckPiece = null
        }
      },
      setPackContainer() {
        let id = this.dataForm.packContainerId
        for (let p of this.packContainerList) {
          if (p.packContainerId == id) {
            this.yfbList.packContainerId = p.packContainerId
            this.yfbList.packContainerName = p.packContainerName
            break
          }
        }
      },
      setCheckPurpose() {
        let id = this.dataForm.checkPurposeId
        for (let c of this.checkPurposeList) {
          if (c.checkPurposeId == id) {
            this.yfbList.checkPurposeId = c.checkPurposeId
            this.yfbList.checkPurposeName = c.checkPurposeName
            break
          }
        }
      },
      setComeUser() {
        this.yfbList.comeUserId = this.dataForm.comeUser.userId
        this.yfbList.comeUserName = this.dataForm.comeUser.name
      },
      setSupplier() {
        this.yfbList.supplierId = this.dataForm.supplier.supplierId
        this.yfbList.supplierName = this.dataForm.supplier.supplierName
      },
      setManufacturer() {
        this.yfbList.manufacturerId = this.dataForm.manufacturer.manufacturerId
        this.yfbList.manufacturerName = this.dataForm.manufacturer.manufacturerName
      }
    }
  }
</script>

<style scoped>
  .el-form-item {
    margin-bottom: 0px !important;
  }

  .el-form-item__content .el-input-group {
    vertical-align: inherit !important;
  }
</style>
